package com.net.xpay.common.service.impl;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserBusinessAuth;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.common.manager.UserBusinessAuthManager;
import com.net.xpay.common.validator.CheckCodeValidator;
import com.net.xpay.common.validator.UserBusinessAuthValidator;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.enums.UserBusinessAuthAuditStatus;
import com.net.xpay.core.model.AdminUserBusinessAuthModel;
import com.net.xpay.core.model.UserBusinessAuthModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.common.exception.BusinessException;
import com.net.xpay.common.service.UserBusinessAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserBusinessAuthServiceImpl implements UserBusinessAuthService {

    @Autowired
    private UserBusinessAuthValidator userBusinessAuthValidator;

    @Autowired
    private UserBusinessAuthManager userBusinessAuthManager;

    @Autowired
    private CheckCodeManager checkCodeManager;

    @Autowired
    private CheckCodeValidator checkCodeValidator;

    @Override
    public void submitBusinessAuthentication(User merchant, UserBusinessAuthModel model) {
        userBusinessAuthValidator.validateUserBusinessAuth(model);
        CheckCode checkCode = checkCodeManager.findByMobileAndType(model.getPhone(), CheckCodeType.BUSINESS_AUTH);
        checkCodeValidator.validateCheckCode(checkCode, model.getCheckCode());
        // 获取当前用户的userId
        Long userId = merchant.getId();
        //更新
        UserBusinessAuth userBusinessAuth = userBusinessAuthManager.getByUserId(userId);
        if (userBusinessAuth != null) {
            BeanCopierUtil.copy(model, userBusinessAuth);
            userBusinessAuth.setUserBusinessAuthAuditStatus(UserBusinessAuthAuditStatus.AUDITING);
            userBusinessAuth.setAuditSubmitTime(LocalDateTime.now());
            userBusinessAuth.setUserId(merchant.getId());
            userBusinessAuthManager.update(userBusinessAuth);
        } else {
            UserBusinessAuth businessAuth = new UserBusinessAuth();
            BeanCopierUtil.copy(model, businessAuth);
            businessAuth.setUserId(merchant.getId());
            businessAuth.setUserBusinessAuthAuditStatus(UserBusinessAuthAuditStatus.AUDITING);
            businessAuth.setAuditSubmitTime(LocalDateTime.now());
            int save = userBusinessAuthManager.save(businessAuth);
            if (save != 1) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业实名提交失败");
            }
        }
    }

    @Override
    public void updateUserBusinessAuth(User merchant, Long id) {
        if (id == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "修改实名认证异常");
        }
        Long userId = merchant.getId();
        UserBusinessAuth userBusinessAuth = userBusinessAuthManager.findById(userId, id);
        if (userBusinessAuth == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业实名认证用户不存在");
        }

        userBusinessAuth.setUserBusinessAuthAuditStatus(UserBusinessAuthAuditStatus.INIT);
        userBusinessAuth.setAuditMessage("");
        userBusinessAuthManager.update(userBusinessAuth);

    }

    @Override
    public void auditSuccess( Long id) {
        if (id == null || id == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "账户企业认证id不可为空");
        }
        UserBusinessAuth userBusinessAuth = userBusinessAuthManager.getById(id);
        UserBusinessAuthAuditStatus status = userBusinessAuth.getUserBusinessAuthAuditStatus();
        if (status != UserBusinessAuthAuditStatus.AUDITING) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "审核状态异常");
        } else {
            userBusinessAuth.setUserBusinessAuthAuditStatus(UserBusinessAuthAuditStatus.NORMAL);
            userBusinessAuth.setAuditTime(LocalDateTime.now());
            userBusinessAuthManager.update(userBusinessAuth);
        }

    }

    @Override
    public void auditFail(AdminUserBusinessAuthModel model) {
        if (Strings.isNullOrEmpty(model.getAuditMessage())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "账户审核失败原因不可为空");
        }
        if (model.getId() == null || model.getId() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "账户企业认证id不可为空");
        }

        UserBusinessAuth userBusinessAuth = userBusinessAuthManager.getById(model.getId());

        UserBusinessAuthAuditStatus status = userBusinessAuth.getUserBusinessAuthAuditStatus();
            if (status != UserBusinessAuthAuditStatus.AUDITING) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "审核状态异常");
            } else {
                userBusinessAuth.setUserBusinessAuthAuditStatus(UserBusinessAuthAuditStatus.FAIL);
                userBusinessAuth.setAuditMessage(model.getAuditMessage());
                userBusinessAuth.setAuditTime(LocalDateTime.now());
               userBusinessAuthManager.update(userBusinessAuth);

            }
        }



}
